<template>
	<view>
		<button @click="chooseImg">点击选择头像</button>
    <image :src="url" style="width:50px;height:50px;
    margin:auto;border:1px solid red"></image>
    {{title}}
    <!-- Storage:异步 StorageSync:同步 -->
    <button @click="setStorage">存储缓存</button>
    <button @click="getStorage">获取缓存</button>
    <button @click="remove">删除abc缓存</button>
    <button @click="clear">清除所有缓存</button>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				url:'',
        title:'阿佛ki尼'
			}
		},
		methods: {
      // 存储缓存
      setStorage(){
        uni.setStorage({
          key:'abc',
          data:{name:'张三',age:'18',sex:'0'}
        })
        uni.setStorageSync('aaa',123)
      },
      // 获取缓存
      getStorage(){
        let t =this
        uni.getStorage({
          key:'abc',
          success(res){
            setTimeout(()=>{
              t.title=res.data
            },2000)
          }
        })
      },
      // 清除指定缓存
      remove(){
        uni.removeStorage({
          key:'abc',
          success(){
            console.log('成功')
          }
        })
        uni.removeStorageSync('abc')
      },
      // 清除所有缓存
      clear(){
        uni.clearStorage()
        uni.clearStorageSync()
      },
			chooseImg(){
        let t = this;
        uni.chooseImage({
          count:1,
          sizeType: ['original', 'compressed'], //可以指定是原图还是压缩图，默认二者都有
          	sourceType: ['album'], //从相册选择
          	success: function (res) {
          		console.log(JSON.stringify(res.tempFilePaths));
              t.url=res.tempFilePaths[0]
          	}
        })
      }
		}
	}
</script>

<style>

</style>
